﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CKS.PLC.Core
{
    public static class ToolsHelper
    {
        public static IPAddress GetLocalIp()
        {
            string name = Dns.GetHostName();
            IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
            foreach (var ip in ipadrlist)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                    return ip;
            }
            return null;
        }

        public static List<IPAddress> GetLocalIps()
        {


            try
            {
                string name = Dns.GetHostName();
                List<IPAddress> ipadrlist = Dns.GetHostAddresses(name).ToList();
                return ipadrlist.FindAll(a => a.AddressFamily == AddressFamily.InterNetwork);
            }
            catch (Exception)
            {
                return new List<IPAddress>();
            }
        }
    }
}